<?php
class CompaniesController extends AppController {

    public $name = 'Companies';
    public $helpers = array('Tagging.Tagging');
    public $cacheAction = array('view/' => '3 hour');
    protected $searchable = array(
        'company_industry_id' => null,
        'company_location_id' => null,
        'company_type_id' => null,
        'created' => null,
        'created_by' => null,
        'modified' => null,
        'modified_by' => null,
        'introduction' => null,
        'name' => null
    );
    public $paginate = array(
        'order' => 'Company.created DESC',
        'contain' => array(
            'CompanyLocation' => array('id', 'name', 'company_count'),
            'CompanyIndustry' => array('id', 'name', 'company_count'),
            'CompanyType' => array('id', 'name', 'company_count'),
            'CreatedUser' => array('id', 'username'),
            'ModifiedUser' => array('id', 'username')
        )
    );

    public function beforeFilter(){
        parent::beforeFilter();
        $this->Auth->allow('view');
        $this->_setCKFinderPath();
    }

    public function index(){
        $this->_justifyPaginator();
        $this->set('companies', $this->paginate());
    }

    public function view($id = null){
        if(!$id){
            $this->Session->setFlash(__('Invalid Company.', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->Company->click($id);
        $this->Company->recursive = 0;
        $this->set('company', $this->Company->read(null, $id));
    }

    public function add(){
        if(!empty($this->data)){
            $this->Company->create();
            if($this->Company->save($this->data)){
                $this->Session->setFlash(__('The Company has been saved', true));
                $this->redirect(array('action'=>'index'));
            }else{
                $this->Session->setFlash(__('The Company could not be saved. Please, try again.', true));
            }
        }
        $companyLocations = $this->Company->CompanyLocation->find('list');
        $companyIndustries = $this->Company->CompanyIndustry->find('list');
        $companyTypes = $this->Company->CompanyType->find('list');
        $clouds = $this->Company->tagCloud();
        $this->set(compact('clouds', 'companyLocations', 'companyIndustries', 'companyTypes'));
    }

    public function edit($id = null){
        if(!$id && empty($this->data)){
            $this->Session->setFlash(__('Invalid Company', true));
            $this->redirect(array('action'=>'index'));
        }
        if(!empty($this->data)){
            if($this->Company->save($this->data)){
                clearCache();
                $this->Session->setFlash(__('The Company has been saved', true));
                $this->redirect(array('action'=>'index'));
            }else{
                $this->Session->setFlash(__('The Company could not be saved. Please, try again.', true));
            }
        }
        if(empty($this->data)){
            $this->data = $this->Company->read(null, $id);
        }
        $companyLocations = $this->Company->CompanyLocation->find('list');
        $companyIndustries = $this->Company->CompanyIndustry->find('list');
        $companyTypes = $this->Company->CompanyType->find('list');
        $clouds = $this->Company->tagCloud();
        $this->set(compact('clouds', 'companyLocations','companyIndustries','companyTypes'));
    }

    public function search(){}

}
?>